<template>
    <div class="program-resources">
        <div class="sidebar">
            <div class="nav-list">
                <div class="head-container">
                    <el-input
                        v-model="nodeName"
                        placeholder="请输入节点名称"
                        clearable
                        size="small"
                        prefix-icon="el-icon-search"
                        style="margin-bottom: 20px"
                    />
                </div>
                <el-tree 
                    :data="chapterOptions"
                    :props="{ children: 'children', label: 'label' }"
                    :expand-on-click-node="false"
                    ref="tree"
                    node-key="id"
                    highlight-current
                    :default-expanded-keys="defaultShowNodes"
                    :indent="14"
                    :filter-node-method="filterNode"
                    @node-click="handleNodeClick"
                    class="custom-tree">
                    <span class="custom-tree-node" slot-scope="{ node }">
                        <span>{{ node.label }}</span>
                    </span>
                </el-tree>
            </div>
        </div>
        <div class="content">
            <div v-if="currentNode">
                <!-- 实验视频 -->
                <div>
                    <div class="resource-title"><i class="el-icon-video-play"></i>实验视频</div>
                    <div v-if="labList.length">
                        <div v-for="item in labList" :key="item.id" class="resource-item">
                            <span class="resource-name">{{ item.crName }}</span>
                            <div class="resource-btn-group">
                                <div class="Btn bgcolor1" @click="playVideo(baseURL + item.crFileUrl)">
                                    <div class="sign">
                                        <i class="el-icon-video-play"></i>
                                    </div>
                                    <div class="text">播放</div>
                                </div>
                                <div class="Btn bgcolor2" @click="downloadFile(item)">
                                    <div class="sign">
                                        <i class="el-icon-download"></i>
                                    </div>
                                    <div class="text">下载</div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div v-else class="no-data">无数据</div>
                </div>
                <!-- 资源文档 -->
                <div>
                    <div class="resource-title"><i class="el-icon-document"></i>资源文档</div>
                    <div v-if="docList.length">
                        <div v-for="item in docList" :key="item.id" class="resource-item">
                            <span class="resource-name">{{ item.crName }}</span>
                            <div class="resource-btn-group">
                                <div class="Btn bgcolor1" @click="previewFile(baseURL + item.crFileUrl)">
                                    <div class="sign">
                                        <i class="el-icon-view"></i>
                                    </div>
                                    <div class="text">预览</div>
                                </div>
                                <div class="Btn bgcolor2" @click="downloadFile(item)">
                                    <div class="sign">
                                        <i class="el-icon-download"></i>
                                    </div>
                                    <div class="text">下载</div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div v-else class="no-data">无数据</div>
                </div>
                <!-- 图集 -->
                <div>
                    <div class="resource-title"><i class="el-icon-picture-outline"></i>图集</div>
                    <div v-if="imageList.length">
                        <div v-for="item in imageList" :key="item.id" class="resource-item">
                            <span class="resource-name">{{ item.crName }}</span>
                            <div class="resource-btn-group">
                                <div class="Btn bgcolor1" @click="previewImage(baseURL + item.crFileUrl)">
                                    <div class="sign">
                                        <i class="el-icon-view"></i>
                                    </div>
                                    <div class="text">预览</div>
                                </div>
                                <div class="Btn bgcolor2" @click="downloadFile(item)">
                                    <div class="sign">
                                        <i class="el-icon-download"></i>
                                    </div>
                                    <div class="text">下载</div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div v-else class="no-data">无数据</div>
                </div>
                <!-- 视频弹窗预览 -->
                <el-dialog :visible.sync="videoDialogVisible" width="70%" top="5vh" :show-close="true" append-to-body custom-class="video-dialog">
                    <video v-if="videoDialogUrl" :src="videoDialogUrl" controls autoplay class="video-player"></video>
                </el-dialog>
                <!-- 图片弹窗预览 -->
                <el-dialog :visible.sync="imgDialogVisible" width="auto" top="10vh" :show-close="true" append-to-body custom-class="img-dialog">
                    <img v-if="imgDialogUrl" :src="imgDialogUrl" style="max-width:80vw;max-height:70vh;display:block;margin:auto;" />
                </el-dialog>
            </div>
            <div v-else>
                <el-empty description="点击左侧节点后查看数据" />
            </div>
        </div>
    </div>
</template>

<script>
import courseRelated from '@/api/courseRelated'

export default {
    name: 'ProgramResources',
    data() {
        return {
            baseURL: process.env.VUE_APP_BASE_API,
            nodeName: '',
            chapterOptions: [],
            courseInfo: null,
            defaultShowNodes: [],
            resourceList: [],
            currentNode: null,
            videoDialogVisible: false,
            videoDialogUrl: '',
            imgDialogVisible: false,
            imgDialogUrl: '',
        }
    },
    created() {
        this.getChapterTree()
    },
    methods: {
        /** 查询章节树 */
        getChapterTree() {
            courseRelated.getCourseRelatedInfo(1).then(response => {
                const courseInfo = response.data
                if (courseInfo == null) return
                courseRelated.treeChapter({ ccParentId: courseInfo.courseId }).then(response => {
                    const { data } = response
                    // console.log('章节树 data:', data)
                    this.chapterOptions = [{
                        id: courseInfo.courseId,
                        label: courseInfo.courseName,
                        children: data
                    }]
                    this.defaultShowNodes = [courseInfo.courseId]
                })
            })
        },
        // 筛选节点
        filterNode(value, data) {
            if (!value) return true
            return data.label.toLowerCase().indexOf(value.toLowerCase()) !== -1
        },
        // 节点点击事件
        handleNodeClick(data, node) {
            const query = { chapterId: data.id }
            courseRelated.getCourseResource(query).then(response => {
                this.resourceList = response.rows || []
            })
            this.currentNode = data
        },
        playVideo(url) {
            this.videoDialogUrl = url
            this.videoDialogVisible = true
        },
        previewFile(url) {
            window.open(url, '_blank')
        },
        downloadFile(item) {
            const link = document.createElement('a');
            link.href = this.baseURL + item.crFileUrl;
            link.download = item.crName || '下载文件';
            link.style.display = 'none';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
        },
        previewImage(url) {
            this.imgDialogUrl = url
            this.imgDialogVisible = true
        }
    },
    computed: {
        labList() {
            return this.resourceList.filter(item => item.resourceCategory === 'lab')
        },
        docList() {
            // 支持ppt, pptx, pdf, doc, docx, xls, xlsx等常见文档类型
            const docTypes = ['ppt', 'pptx', 'pdf', 'doc', 'docx', 'xls', 'xlsx']
            return this.resourceList.filter(item =>
                item.resourceCategory !== 'video' &&
                docTypes.includes((item.crFileType || '').toLowerCase())
            )
        },
        imageList() {
            const imgTypes = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp']
            return this.resourceList.filter(item =>
                item.resourceCategory !== 'video' &&
                imgTypes.includes((item.crFileType || '').toLowerCase())
            )
        }
    },
    watch: {
        nodeName(val) {
            this.$refs.tree.filter(val)
        }
    }
}
</script>

<style lang="scss" scoped>
.program-resources {
    display: flex;
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
}

.sidebar {
    width: 20%;
    height: 100%;
    box-sizing: border-box;
    background: rgb(68,206,246);
    background: linear-gradient(135deg, rgba(68,206,246,1) 0%, rgba(94,129,255,1) 40%);
    color: white;
    padding: 20px 0;
    display: flex;
    flex-direction: column;
    border-top-left-radius: 20px;
    overflow-y: auto;

    .logo {
        padding: 20px 30px;
        h2 {
            margin: 0;
            font-size: 24px;
            font-weight: bold;
        }
    }

    .nav-list {
        margin-top: 20px;
        
        .head-container {
            padding: 0 20px;
        }
        
        .custom-tree {
            background: transparent;
            color: white;
            
            :deep(.el-tree-node__content) {
                height: 50px;
                padding: 0 30px;
            margin: 5px 20px;
            margin-right: 0px;
            border-radius: 20px 0px 0px 20px;
                transition: all 0.3s ease;
            
            &:hover {
                background: rgba(255, 255, 255, 0.1);
                }
            }
            
            :deep(.el-tree-node.is-current > .el-tree-node__content) {
                background: white;
                color: #5e81ff;
                box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            }
            
            :deep(.el-tree-node__label) {
                font-size: 16px;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }
        }
    }
}

.content {
    width: 80%;
    padding: 20px;
    box-sizing: border-box;
    padding-right: 0;
    height: 100%;
    overflow-y: auto;
    .content_top{
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-around;
        .content_top_item{
            width: 23%;
            padding: 30px 0;
            text-align: center;
            color: #fff;
            font-size: 18px;
            font-weight: bold;
            cursor: pointer;
            border-radius: 10px;
        }
    }
    .el-empty {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        min-height: 60vh;
    }
}
.color1{
    background: #e6cde3;
}
.color2{
    background: #95e1d3
}
.color3{
    background: #fce38a
}
.resource-title {
    font-size: 20px;
    font-weight: bold;
    margin: 32px 0 16px 0;
    color: #222;
    letter-spacing: 1px;
}
.no-data {
    color: #bbb;
    text-align: center;
    margin: 16px 0 32px 0;
    font-size: 15px;
}
.resource-item {
    padding: 8px 20px;
    border-bottom: 1px solid #f0f0f0;
    font-size: 16px;
    color: #333;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    box-sizing: border-box;
}
.resource-video-box {
    display: flex;
    align-items: center;
    margin-bottom: 18px;
    background: #f8faff;
    border-radius: 8px;
    padding: 12px 18px;
    box-shadow: 0 2px 8px 0 rgba(0,0,0,0.03);
}
.video-preview {
    margin-right: 18px;
    border-radius: 8px;
    background: #000;
}
.resource-info {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.resource-name {
    font-size: 16px;
    font-weight: 500;
    margin-bottom: 4px;
    color: #222;
}
.resource-img-box {
    display: flex;
    align-items: center;
    margin-bottom: 18px;
    background: #f8faff;
    border-radius: 8px;
    padding: 12px 18px;
    box-shadow: 0 2px 8px 0 rgba(0,0,0,0.03);
}
.img-preview {
    width: 80px;
    height: 60px;
    object-fit: cover;
    margin-right: 18px;
    border-radius: 6px;
    cursor: pointer;
    background: #eee;
}
.img-dialog {
    background: transparent;
    box-shadow: none;
}
.resource-btn-group {
    display: flex;
    align-items: center;
    margin-left: auto;
}
.Btn {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 35px;
  height: 35px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition-duration: 0.3s;
  box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.199);
  margin-left: 10px;
}
.sign {
  width: 100%;
  transition-duration: 0.3s;
  display: flex;
  align-items: center;
  justify-content: center;
}
.sign i {
  color: #fff;
  .el-icon-video-play{
    width: 20px;
    height: 20px;
  }
  .el-icon-download{
    width: 20px;
    height: 20px;
  }
  .el-icon-view{
    width: 20px;
    height: 20px;
  }
}
.text {
  position: absolute;
  right: 0%;
  width: 0%;
  opacity: 0;
  color:#fff;
  font-size:14px;
  font-weight: 600;
  transition-duration: 0.1s;
}
.Btn:hover {
  width: 75px;
  border-radius: 40px;
  transition-duration: 0.3s;
}
.Btn:hover .sign {
  width: 30%;
  transition-duration: 0.3s;
  padding:0 5px;
}
.Btn:hover .text {
  opacity: 1;
  width: 70%;
  transition-duration: 0.1s;
  padding-left: 15px;
  box-sizing: border-box;
}
.Btn:active {
  transform: translate(2px, 2px);
}
.bgcolor1{
    background-color: #89c3eb;
}
.bgcolor2{
    background-color: #b0a4e3;
}
.video-dialog {
  .video-player {
    width: 100%;
    height: auto;
    max-height: 70vh;
    object-fit: contain;
  }
  .el-dialog__body {
    padding: 0;
    background: #000;
  }
  .el-dialog__footer {
    text-align: center;
    background: #f5f5f5;
  }
}
::v-deep .el-dialog.video-dialog {
  margin-top: 5vh !important;
  margin-left: 21% !important;
}
::v-deep .el-dialog__header{
  background: #262626;
}
::v-deep .el-dialog__body{
  background: #262626;
}
::v-deep .el-dialog__footer{
  background: #262626;
}
</style>